package cn.hcbycloud.auth.front.converter;


import cn.hcbycloud.auth.bo.CustInfoBO;
import cn.hcbycloud.auth.bo.UserRegisterBO;
import cn.hcbycloud.auth.bo.UserRegisterResultBO;
import cn.hcbycloud.auth.front.dto.FrontPermissionDTO;
import cn.hcbycloud.auth.front.dto.FrontRegisterUserRequestDTO;
import cn.hcbycloud.auth.front.dto.FrontRegisterUserResponseDTO;
import cn.hcbycloud.auth.front.dto.FrontRoleDTO;
import cn.hcbycloud.auth.model.Cust;
import cn.hcbycloud.auth.model.User;
import cn.hcbycloud.auth.model.UserLogin;
import cn.hcbycloud.common.core.constant.CustMainIdTypeEnum;
import cn.hcbycloud.common.core.constant.CustomerTypeEnum;
import cn.hcbycloud.common.core.constant.SexTypeEnum;
import cn.hcbycloud.common.core.constant.UserNameTypeEnum;
import cn.hcbycloud.common.core.exception.CustomizeException;
import cn.hcbycloud.common.core.service.tree.TreeNodeConfigService;
import cn.hcbycloud.common.core.service.tree.TreeService;
import cn.hcbycloud.common.core.utils.ObjectUtils;
import cn.hcbycloud.common.core.utils.RegexUtils;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 用户基本信息表
 *
 * @author gaoheng
 * @date 2022-01-24 14:52:06
 */
@Component
public class FrontUserConverter {
    /**
     * 树型列表配置服务
     */
    @Autowired
    TreeNodeConfigService treeNodeConfigService;
    /**
     * 树形列表生成服务
     */
    @Autowired
    TreeService treeService;

    /**
     * 注册个人客户转换方法
     *
     * @param requestDTO
     * @return
     */
    public UserRegisterBO requestToModelOfPersonCust(FrontRegisterUserRequestDTO requestDTO) {
        UserRegisterBO userRegisterBO = new UserRegisterBO();
        List<UserLogin> userLoginList = CollUtil.newArrayList();
        User user = new User();
        /** 开始进行校验 */
        if (StrUtil.isBlank(requestDTO.getUserName()) && StrUtil.isBlank(requestDTO.getMobilePhone())
                && StrUtil.isBlank(requestDTO.getIdCard()) && StrUtil.isBlank(requestDTO.getWechatOpenId())
                && StrUtil.isBlank(requestDTO.getQq()) && StrUtil.isBlank(requestDTO.getMail())) {
            throw new CustomizeException("最少填写一个用户唯一标识，用户名|手机号|身份证号|邮箱|微信ID|QQ");
        }
        if (StrUtil.isNotBlank(requestDTO.getName())) {
            if (!RegexUtils.isNickname(requestDTO.getName())) {
                throw new CustomizeException("用户姓名校验不通过：限定10个字符，支持中英文、数字、减号或下划线");
            }
            user.setName(requestDTO.getName());
        }
        if (StrUtil.isNotBlank(requestDTO.getUserName())) {
            if (!RegexUtils.isEngNumUnderline(requestDTO.getUserName())) {
                throw new CustomizeException("用户名校验不通过：由数字、26个英文字母或者下划线组成的字符串");
            }
            userLoginList.add(setUserLogin(requestDTO.getUserName(), UserNameTypeEnum.USER_USER_NAME_TYPE));
        }
        if (StrUtil.isNotBlank(requestDTO.getMobilePhone())) {
            if (!RegexUtils.isMobile(requestDTO.getMobilePhone())) {
                throw new CustomizeException("手机号校验不通过");
            }
            userLoginList.add(setUserLogin(requestDTO.getMobilePhone(), UserNameTypeEnum.USER_MOBILE_PHONE_TYPE));
        }
        if (StrUtil.isNotBlank(requestDTO.getIdCard())) {
            if (!RegexUtils.isIdCard(requestDTO.getIdCard())) {
                throw new CustomizeException("身份证号校验不通过");
            }
            userLoginList.add(setUserLogin(requestDTO.getIdCard(), UserNameTypeEnum.USER_ID_CARD_TYPE));
        }
        if (StrUtil.isNotBlank(requestDTO.getMail())) {
            if (!RegexUtils.isEmail(requestDTO.getMail())) {
                throw new CustomizeException("邮箱校验不通过");
            }
            userLoginList.add(setUserLogin(requestDTO.getMail(), UserNameTypeEnum.USER_MAIL_TYPE));
        }
        if (StrUtil.isNotBlank(requestDTO.getNickname())) {
            if (!RegexUtils.isNickname(requestDTO.getNickname())) {
                throw new CustomizeException("昵称校验不通过，限10个字符，支持中英文、数字、减号或下划线");
            }
            user.setNickname(requestDTO.getNickname());
        }
        if (ObjectUtil.isNull(SexTypeEnum.getSexTypeEnum(requestDTO.getSex()))) {
            throw new CustomizeException("性别编码错误，0:未知 | 1:男 | 2:女");
        }
        user.setSex(requestDTO.getSex());
        if (StrUtil.isNotBlank(requestDTO.getPassword())) {
            if (StrUtil.isBlank(requestDTO.getConfirmPassword()) ||
                    !StrUtil.equals(requestDTO.getPassword(), requestDTO.getConfirmPassword())) {
                throw new CustomizeException("确认密码与密码不一致");
            }
        }
        if (StrUtil.isBlank(requestDTO.getCustType()) ||
                !ObjectUtil.equal(CustomerTypeEnum.CUSTOMER_TYPE_PERSON,CustomerTypeEnum.getCustomerTypeEnum(requestDTO.getCustType()))) {
            throw new CustomizeException("客户类型校验不通过,必须为个人客户类型");
        }
        user.setPassword(requestDTO.getPassword());
        userRegisterBO.setUser(user);
        userRegisterBO.setUserLoginList(userLoginList);
        /** 校验集团客户挂载信息 */
        if (StrUtil.isNotBlank(requestDTO.getCreditCode())) {
            if (!RegexUtils.isJigouCode(requestDTO.getCreditCode())) {
                throw new CustomizeException("企业统一社会信用代码校验不通过");
            }
            CustInfoBO custInfoBO = new CustInfoBO();
            Cust cust = new Cust();
            cust.setMainId(requestDTO.getCreditCode());
            cust.setMainType(CustMainIdTypeEnum.CUST_CREDIT_CODE_TYPE.getCode());
            // 将客户类型设置为个人客户
            cust.setCustType(CustomerTypeEnum.CUSTOMER_TYPE_PERSON.getCode());
            custInfoBO.setCust(cust);
            userRegisterBO.setCustOfBusinessBo(custInfoBO);
        }
        /* 默认赋权角色编码*/
        userRegisterBO.setRoleCodes(requestDTO.getRoleCodes());
        return userRegisterBO;
    }

    /**
     * 将用户注册信息返回
     *
     * @param userRegisterResultBO
     * @return
     */
    public FrontRegisterUserResponseDTO modelToResponse(UserRegisterResultBO userRegisterResultBO){
        FrontRegisterUserResponseDTO frontRegisterUserResponseDTO = new FrontRegisterUserResponseDTO();
        // 用户姓名
        frontRegisterUserResponseDTO.setName(userRegisterResultBO.getName());
        // JWTToken
        frontRegisterUserResponseDTO.setJwtToken(userRegisterResultBO.getJwtToken());
        // 昵称
        frontRegisterUserResponseDTO.setNickname(userRegisterResultBO.getNickname());
        // 头像
        frontRegisterUserResponseDTO.setPhoto(userRegisterResultBO.getPhoto());
        // 用户注册之后默认配置的角色列表
        List<FrontRoleDTO> frontRoleDTOList = ObjectUtils.cloneObjList(userRegisterResultBO.getRoleBOList(),FrontRoleDTO.class);
        frontRegisterUserResponseDTO.setRoleList(frontRoleDTOList);
        // 用户注册之后默认配置的权限列表
        List<FrontPermissionDTO> frontPermissionDTOList = ObjectUtils.cloneObjList(userRegisterResultBO.getPermissionContainResourceBo().getPermissionBoList(),
                FrontPermissionDTO.class);
        frontRegisterUserResponseDTO.setPermissionList(frontPermissionDTOList);
        /** 用户注册之后默认配置的资源行为列表，由于为树形结构，需要转换为树形列表 */
        TreeNodeConfig treeNodeConfig = treeNodeConfigService.getTreeNodeConfig("resourceId", "parentId", 4, "children");
        List<Tree<String>> resourcesTree = treeService.buildTreeList(userRegisterResultBO.getPermissionContainResourceBo().getResourceActionBoList(), treeNodeConfig, "0");
        frontRegisterUserResponseDTO.setResource(resourcesTree);
        return frontRegisterUserResponseDTO;
    }

    /**
     * 组装userLogin对象
     *
     * @param userName
     * @param userNameTypeEnum
     * @return
     */
    private UserLogin setUserLogin(String userName, UserNameTypeEnum userNameTypeEnum) {
        UserLogin userLogin = new UserLogin();
        userLogin.setUserNameType(userNameTypeEnum.getCode());
        userLogin.setUserName(userName);
        return userLogin;
    }
}
